package org.larkdoc.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.larkdoc.config.ReadConfig;
import org.larkdoc.constant.ViewConts;
import org.larkdoc.model.entity.UserInfo;
import org.larkdoc.model.vo.RespBaseVO;
import org.larkdoc.model.vo.UserVO;
import org.larkdoc.service.UserService;
import org.larkdoc.util.EncryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户信息控制类
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    UserService userService;
    
    @Autowired
    ReadConfig  readConfig;
    
    @RequestMapping("/register")
    public RespBaseVO register(UserInfo user) {
        RespBaseVO respVo = new RespBaseVO();
        if (checkUser(user)) {
            // TODO 帐号验证，密码验证等
            UserInfo dbUser = userService.getByAccount(user.getAccount());
            if (null == dbUser) {
                user.setPassword(EncryUtil.md5Encry(user.getPassword(), user.getAccount() + readConfig.getPubSalt()));
                if (userService.insertSelective(user) != 1) {
                    respVo.setRetCode(ViewConts.Code.FAILURE);
                    respVo.setRetMsg(ViewConts.Msg.FAILURE);
                }
            } else {
                respVo.setRetCode(ViewConts.Code.FAILURE);
                respVo.setRetMsg("新增失败，用户已存在.");
            }
        }
        return respVo;
    }
    
    @RequestMapping("/delete/{id}")
    public RespBaseVO delete(@PathVariable int id) {
        // 没有删除需求
        return new RespBaseVO();
    }
    
    @RequestMapping("/get/{id}")
    public RespBaseVO get(@PathVariable int id) {
        RespBaseVO respVo = new RespBaseVO();
        if (id > 0) {
            respVo.setRetVal(userService.selectByPrimaryKey(id));
        }
        return respVo;
    }
    
    @RequestMapping("/update")
    public RespBaseVO update(UserInfo user) {
        RespBaseVO respVo = new RespBaseVO();
        // TODO 只支持用户更新自己帐号
        if (null != user) {
            if (userService.insertSelective(user) != 1) {
                respVo.setRetCode(ViewConts.Code.FAILURE);
                respVo.setRetMsg(ViewConts.Msg.FAILURE);
            }
        }
        return respVo;
    }
    
    @RequestMapping("queryByNameOrAccount")
    @ResponseBody
    public RespBaseVO queryByNameOrAccount(@RequestParam("condition") String condition){
        RespBaseVO respVo = new RespBaseVO();
        if(StringUtils.isNoneBlank(condition)){
            List<UserInfo> query = userService.queryByNameOrAccount(condition);
            if(CollectionUtils.isNotEmpty(query)){
                List<UserVO> result = new ArrayList<UserVO>(query.size());
                UserVO userVo = null;
                // 筛选数据
                for (UserInfo user : query) {
                    userVo = new UserVO();
                    userVo.setId(user.getId());
                    userVo.setName(user.getName());
                    userVo.setAccount(user.getAccount());
                    result.add(userVo);
                }
                respVo.setRetVal(result);
            }
        }
        return respVo;
    }
    
    private boolean checkUser(UserInfo user) {
        boolean result = true;
        if (null == user) {
            result = false;
        } else {
            if (StringUtils.isBlank(user.getAccount()) || StringUtils.isBlank(user.getPassword())) {
                result = false;
            }
        }
        return result;
    }
}
